package com.tbit.uqbike.webmanager.controller;

import com.github.pagehelper.PageInfo;
import com.tbit.uqbike.enums.SystemCodeEnum;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.Machine;
import com.tbit.uqbike.object.pojo.MachineBatteryBound;
import com.tbit.uqbike.object.value.Page;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.base.MachineService;
import com.tbit.uqbike.service.business.BatteryService;
import com.tbit.uqbike.service.business.MachineBatteryBoundService;
import com.tbit.uqbike.webmanager.util.PageHelperUtil;
import com.tbit.utils.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

/**
 * 车辆电池绑定表(MachineBatteryBound)表控制层
 *
 * @author jing
 * @since 2020-12-04 10:32:14
 */
@RestController
@RequestMapping("/machinebatterybound")
public class MachineBatteryBoundController {
    /**
     * 服务对象
     */
    @Resource
    private MachineBatteryBoundService machineBatteryBoundService;
    @Resource
    private MachineService machineService;
    @Resource
    private BatteryService batteryService;

    /**
     * 通过主键查询单条数据
     *
     * @param machineBatteryBoundId 主键
     * @return 单条数据
     */
    @PostMapping("/selectList")
    public JsonResult selectList(Integer machineBatteryBoundId) {
        return JsonResult.ok(this.machineBatteryBoundService.queryById(machineBatteryBoundId));
    }

    @PostMapping("/add")
    public JsonResult add(MachineBatteryBound machineBatteryBound) {
        /* 校验 */
        Assert.notNull(machineBatteryBound.getBikeModelId(), "ver.batteryChargeModel.notNull");
        Assert.notNull(machineBatteryBound.getUserCode(), "ver.userCode.notNull");
        Assert.notNull(machineBatteryBound.getAccountId(), "ver.accountId.notNull");
        Assert.notNull(machineBatteryBound.getBatteryNO(), "ver.batteryNO.notNull");
        machineBatteryBound.setBoundTime(new Date());
        Boolean flag = machineBatteryBoundService.queryByUserCode(machineBatteryBound.getUserCode());
        if (flag) {
            throw new BaseException(SystemCodeEnum.USERCODE_ALREADY_EXIST);
        }
        flag = batteryService.queryByBatteryNO(machineBatteryBound.getBatteryNO());
        if (!flag) {
            throw new BaseException(SystemCodeEnum.BATTERYNO_NOT_EXIST);
        }
        Machine machine = machineService.getByUserCode(machineBatteryBound.getUserCode());
        if (null == machine) {
            throw new BaseException(SystemCodeEnum.MACHINE_NOT_EXIST);
        }
        machineBatteryBound.setMachineId(machine.getMachineId());
        machineBatteryBound.setMachineNO(machine.getMachineNO());
        machineBatteryBoundService.insert(machineBatteryBound);
        return JsonResult.ok();
    }

    /**
     * @param machineBatteryBound
     * @return com.tbit.uqbike.object.view.JsonResult
     * @Description: 批量添加
     * @createDate 2020/12/4 15:35
     */
    @PostMapping("/addBatch")
    public JsonResult addBatch(MachineBatteryBound machineBatteryBound) {
        Assert.notNull(machineBatteryBound.getBikeModelId(), "ver.batteryChargeModel.notNull");
        Assert.notNull(machineBatteryBound.getAccountId(), "ver.accountId.notNull");
        List<String> data = machineBatteryBound.getData();
        if (null == data) {
            throw new BaseException(SystemCodeEnum.READ_FILE_ERROR);
        }
        List<MachineBatteryBound> machineBatteryBounds = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        data.stream().forEach(params -> {
            if (params != null) {
                String[] split = params.split(",");
                String usercode = split[0];
                String batteryNO = split[1];
                /**该车辆是否添加，true添加了*/
                Boolean flag = machineBatteryBoundService.queryByUserCode(usercode);
                /**该编号是否存在*/
                Boolean b = batteryService.queryByBatteryNO(batteryNO);
                /**为添加的车辆才添加*/
                if (!map.containsKey(usercode) && !flag && b) {
                    /**判断车辆是否存在*/
                    Machine machine = machineService.getByUserCode(usercode);
                    /**该表是否已添加该电池*/
                    MachineBatteryBound machineBatteryBound1 = machineBatteryBoundService.queryByNO(batteryNO);
                    if (null != machine && null == machineBatteryBound1) {
                        machineBatteryBounds.add(new MachineBatteryBound(machine.getMachineId(), machineBatteryBound.getAccountId(), machine.getMachineNO(), machine.getUserCode(), machineBatteryBound.getBikeModelId(), batteryNO, new Date()));
                        map.put(usercode, usercode);
                    }
                }
            }
        });
        machineBatteryBoundService.insertBatch(machineBatteryBounds);
        map.clear();
        return JsonResult.ok();

    }


    /**
     * @param machineBatteryBound
     * @return com.tbit.uqbike.object.view.JsonResult
     * @Description: 修改
     * @createDate 2020/12/4 15:35
     */
    @PostMapping("/edit")
    public JsonResult edit(MachineBatteryBound machineBatteryBound) {
        Assert.notNull(machineBatteryBound.getMachineBatteryBoundId(), "ver.machineBatteryBoundId.notNull");
        Assert.notNull(machineBatteryBound.getBikeModelId(), "ver.bikeModelId.notNull");
        Assert.notNull(machineBatteryBound.getUserCode(), "ver.userCode.notNull");
        Assert.notNull(machineBatteryBound.getAccountId(), "ver.accountId.notNull");
        Assert.notNull(machineBatteryBound.getBatteryNO(), "ver.batteryNO.notNull");
        /**移除未添加的和不存在的车辆*/
        if (!batteryService.queryByBatteryNO(machineBatteryBound.getBatteryNO())) {
            throw new BaseException(SystemCodeEnum.BATTERY_NOT_EXIST);
        }
        if (null == machineService.getByUserCode(machineBatteryBound.getUserCode())) {
            throw new BaseException(SystemCodeEnum.MACHINE_NOT_EXIST);
        }
        MachineBatteryBound machineBatteryBound1 = machineBatteryBoundService.queryByCode(machineBatteryBound.getUserCode());
        if (machineBatteryBound1 != null && !machineBatteryBound1.getMachineBatteryBoundId().equals(machineBatteryBound.getMachineBatteryBoundId()) && machineBatteryBound.getUserCode() == machineBatteryBound1.getUserCode()) {
            throw new BaseException(SystemCodeEnum.USERCODE_ALREADY_EXIST);
        }
        machineBatteryBound1 = machineBatteryBoundService.queryByCode(machineBatteryBound.getBatteryNO());
        if (machineBatteryBound1 != null && !machineBatteryBound1.getMachineBatteryBoundId().equals(machineBatteryBound.getMachineBatteryBoundId()) && machineBatteryBound.getBatteryNO() == machineBatteryBound1.getBatteryNO()) {
            throw new BaseException(SystemCodeEnum.USERCODE_ALREADY_EXIST);
        }
        machineBatteryBoundService.update(machineBatteryBound);
        return JsonResult.ok();
    }

    @PostMapping("/delete")
    public JsonResult del(MachineBatteryBound machineBatteryBound) {
        Assert.notNull(machineBatteryBound.getMachineBatteryBoundId(), "ver.machineBatteryBoundId.notNull");
        machineBatteryBoundService.deleteById(machineBatteryBound.getMachineBatteryBoundId());
        return JsonResult.ok();
    }

    /**
     * @param machineBatteryBound
     * @return com.tbit.uqbike.object.view.JsonResult
     * @Description: 批量删除
     * @createDate 2020/12/4 17:20
     */
    @PostMapping("/deleteBatch")
    public JsonResult delBatch(MachineBatteryBound machineBatteryBound) {
        Assert.notNull(machineBatteryBound.getUserCodes(), "ver.userCode.notNull");
        machineBatteryBound.setList(Arrays.asList(machineBatteryBound.getUserCodes().split(",")));
        machineBatteryBoundService.deleteByUserCode(machineBatteryBound);
        return JsonResult.ok();
    }


    @PostMapping("/queryPage")
    public JsonResult queryPage(MachineBatteryBound machineBatteryBound) {
        machineBatteryBound.setPage(new Page(machineBatteryBound.getPageNO(), machineBatteryBound.getRowCount()));
        PageHelperUtil.initPage(machineBatteryBound.getPage());
        PageInfo pageInfo = new PageInfo(machineBatteryBoundService.queryPage(machineBatteryBound));
        return JsonResult.ok(pageInfo.getList(), (int) pageInfo.getTotal());
    }
}